package com.blockmeta.bbs.businesslibrary.popwindow;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.widget.TextView;

import com.blockmeta.bbs.baselibrary.utils.DimentionsUtil;
import com.blockmeta.bbs.businesslibrary.R;
import com.blockmeta.bbs.businesslibrary.arouter.RouterHelper;
import com.blockmeta.bbs.businesslibrary.arouter.RouterPath;
import com.blockmeta.bbs.businesslibrary.business.UmengEvent;
import com.blockmeta.bbs.businesslibrary.constant.Constant;

import razerdp.basepopup.BasePopupWindow;

/**
 * @author AirThor
 * @date 2018/10/10.
 */
public class TopPopupWindow extends BasePopupWindow implements View.OnClickListener{
    private View mPopView;
    public TextView mCoinCount;
    private final Handler mHandler;
    private static final int MSG_TIMEOUT = 0;
    private static final int DURATION = 3000;
    private Context mContext;
    public TopPopupWindow(Context context) {
        super(context, ViewGroup.LayoutParams.MATCH_PARENT, DimentionsUtil.dp2px(context,52));
        mContext = context;
        mHandler = new Handler(Looper.getMainLooper(), new Handler.Callback() {
            @Override
            public boolean handleMessage(Message msg) {
                switch (msg.what){
                    case MSG_TIMEOUT:
                        dismiss();
                        return true;
                    default:
                        return false;
                }
            }
        });
    }

    @Override
    protected Animation initShowAnimation() {
        return getTranslateVerticalAnimation(-1f, 0f, 500);
    }

    @Override
    protected Animation initExitAnimation() {
        return getTranslateVerticalAnimation(0f, -1f, 500);
    }

    @Override
    public View getClickToDismissView() {
        return null;
    }

    @Override
    public View onCreatePopupView() {
        mPopView=LayoutInflater.from(getContext()).inflate(R.layout.pop_task_hint, null);
        View clickView = mPopView.findViewById(R.id.btn_go);
        mCoinCount = mPopView.findViewById(R.id.tv_2);
        clickView.setOnClickListener(this);
        return mPopView;
    }

    @Override
    public View initAnimaView() {
        return mPopView.findViewById(R.id.anim_view);
    }

    @Override
    public void onDismiss() {
        super.onDismiss();
        mHandler.removeCallbacksAndMessages(this);
    }

    @Override
    public void showPopupWindow() {
        super.showPopupWindow();
        delayDismiss();
    }

    private void delayDismiss() {
        mHandler.sendEmptyMessageDelayed(MSG_TIMEOUT,DURATION);
    }

    @Override
    public void showPopupWindow(View v) {
        super.showPopupWindow(v);
        delayDismiss();
    }

    @Override
    public void onClick(View v) {
        RouterHelper.startActivity(RouterPath.TASK_CENTER_ACTIVITY);
        UmengEvent.eventStatistics(mContext, Constant.U_MINE_task_snackbar_go,null);
    }
}
